<template>
  <el-row :gutter="20">
    <el-col :span="12" :xs="24">
      <AppSiteSimpleList @row-click="handleAppSiteSimpleListRowClick" />
    </el-col>
    <el-col :span="12" :xs="24">
      <!-- 搜索工作栏 -->
      <div class="head-container">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入栏目标题"
          clearable
          @keyup.enter="handleQuery"
          class="mb-20px"
        >
          <template #prefix>
            <Icon icon="ep:search" />
          </template>
        </el-input>
      </div>
      <!-- 列表 -->
      <div class="head-container">
        <el-table
          v-loading="loading"
          :data="list"
          :stripe="true"
          row-key="id"
          :default-expand-all="isExpandAll"
          v-if="refreshTable"
          :show-overflow-tooltip="true"
          @row-click="handleRowClick"
          highlight-current-row
        >
          <el-table-column label="栏目标题" align="center" prop="title" />
          <el-table-column label="导航栏" align="center" prop="navBar">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.navBar" />
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-col>
  </el-row>
</template>

<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { ArticleCategoryApi, ArticleCategoryVO } from '@/api/cms/articlecategory'
import AppSiteSimpleList from '@/views/cms/appsite/AppSiteSimpleList.vue'
import { AppSiteVO } from '@/api/cms/appsite'
import { handleTree } from '@/utils/tree'

/** 文章栏目 列表 */
defineOptions({ name: 'ArticleCategoryTree' })

const isExpandAll = ref(true) // 是否展开，默认全部展开
const refreshTable = ref(true) // 重新渲染表格状态

/** 处理某一行的点击 */
const handleRowClick = (row: ArticleCategoryVO) => {
  emits('row-click', row)
}
const emits = defineEmits(['row-click'])

const loading = ref(true) // 列表的加载中
const list = ref<ArticleCategoryVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: -1,
  appSiteId: 0,
  appId: '',
  parentId: undefined,
  title: undefined,
  subTitle: undefined,
  navBar: undefined,
  status: undefined,
  viewNum: undefined,
  createTime: []
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ArticleCategoryApi.getArticleCategoryPage(queryParams)
    list.value = handleTree(data.list)
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const handleAppSiteSimpleListRowClick = (row: AppSiteVO) => {
  queryParams.appSiteId = row.id
  queryParams.appId = row.appId
  handleQuery()
  handleRowClick({
    appSiteId: row.id, // 站点编号
    appId: row.appId // 站点标识
  } as ArticleCategoryVO)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
